package com.party.service.exam.impl;

import com.party.common.Page;
import com.party.mapper.ExamMapper;
import com.party.mapper.User2ExamMapper;
import com.party.model.Exam;
import com.party.model.ExamExample;
import com.party.model.User2Exam;
import com.party.model.User2ExamExample;
import com.party.service.exam.ExamService;
import com.party.service.exam.ExamTestService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-9
 * Time: 下午8:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ExamServiceImpl implements ExamService {

    @Resource
    ExamMapper examMapper;

    @Resource
    ExamTestService examTestService;
    @Resource
    User2ExamMapper user2ExamMapper;
    @Override
    public int addExam(Exam exam) {
        int num = examMapper.insertSelective(exam);
        return num;
    }

    @Override
    public int deleteExamByPrimaryKey(Integer id) {
        int num = examMapper.deleteByPrimaryKey(id);
        return num;
    }

    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += examMapper.deleteByPrimaryKey(Integer.parseInt(roid));
                examTestService.deleteExamByIds(ids);
            }
        }
        return num;
    }

    @Override
    public Exam getExamByPrimaryKey(Integer id) {
        Exam exam = examMapper.selectByPrimaryKey(id);
        return exam;
    }

    @Override
    public int updateExamByPrimaryKey(Exam Exam) {
        int num = examMapper.updateByPrimaryKeySelective(Exam);
        return num;
    }

    @Override
    public  List<Exam> getAllExam() {
        ExamExample examExample = new ExamExample();
        List<Exam> listExam=examMapper.selectByExample(examExample);
        return listExam;
    }

    @Override
    public Page getExamTestList(Exam exam, int pageSize, int pageNumber) {
        ExamExample examExample = new ExamExample();
        /**
         * 可以添加查询条件
         */
        if(exam != null)
        {
            ExamExample.Criteria criteria = examExample.createCriteria();
            if(exam.getName() != null && !"".equals(exam.getName()))
            {
                criteria.andNameLike("%"+exam.getName()+"%");
            }
            if(exam.getTesttime() != null)
            {
                criteria.andTesttimeEqualTo(exam.getTesttime());
            }
        }
        examExample.setOrderByClause("creatTime desc");

        //获取总的记录数量
        int count = examMapper.countByExample(examExample);
        //获取指定的页码数据
        examExample.setBeginNum((pageNumber - 1) * pageSize);
        examExample.setEndNum(pageSize);
        List<Exam> list = examMapper.getListPage(examExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public List<Exam> getByUid(Integer uid) {
        if(uid != null && uid > 0)
        {
            User2ExamExample examExample = new User2ExamExample();
            User2ExamExample.Criteria criteria = examExample.createCriteria();
            criteria.andUidEqualTo(uid);
            List<User2Exam> user2ExamList = this.user2ExamMapper.selectByExample(examExample);
            List<Exam> examList = new ArrayList<Exam>();
            if(user2ExamList!=null && user2ExamList.size() >0)
            {
                for(User2Exam user2Exam:user2ExamList)
                {
                    Exam e = this.examMapper.selectByPrimaryKey(user2Exam.getExamid());
                    if(e != null)
                    {
                        e.setObtainScore(user2Exam.getScore());
                        examList.add(e);
                    }
                }
            }
            return examList;
        }
        return null;
    }
}
